import {
  getDictByBzdm,
  buildJxjgTree,
  buildLyjgTree,
  buildLyjgTree2,
  getAllLyfl,
  getAllLy,
  getAllLc,
  getAllFj,
  getAllCw,
  getZzjgYxAll,
  getZzjgZyAll,
  buildLyflAndLyTree,
  buildLcAndFjTree,
  findJzgxxList,
  printInterFace
} from '@/api/common.js'

const state = {
  isLoading: false, // loading层
  reqNum: 0, // 请求数量
  reqType: 0, // 请求类别
  xbList: [], // 性别list
  cwfList: [], // 床位费list
  xnList: [], // 学年list
  jxjgTree: [], // 教学机构树形数据
  lyTree: [], // 楼宇树形数据
  xzList: [], // 学制list
  ssxqList: [], // 所属校区list
  // lyTree2: [] // 楼宇树形数据-到床位
}
const mutations = {
  SET_REQ_NUM: (state, data) => {
    state.reqNum += data
  },
  SET_REQ_TYPE: (state, data = 0) => {
    state.reqType = data
  },
  SET_XB_LIST: (state, data) => {
    state.xbList = data
  },
  SET_CWF_LIST: (state, data) => {
    state.cwfList = data
  },
  SET_XN_LIST: (state, data) => {
    state.xnList = data
  },
  SET_JXJG_TREE: (state, data) => {
    state.jxjgTree = data
  },
  SET_LY_TREE: (state, data) => {
    state.lyTree = data
  },
  SET_XZ_LIST: (state, data) => {
    state.xzList = data
  },
  // SET_XJ_LIST: (state, data) => {
  //   state.xjList = data
  // },
  SET_SSXQ_LIST: (state, data) => {
    state.ssxqList = data
  }
  // SET_LY_TREE_2: (state, data) => {
  //   state.lyTree2 = data
  // }
}
const actions = {
  getDictByBzdm({ commit, state }, data) {
    return new Promise((resolve, reject) => {
      if (state.xbList && state.xbList.length) {
        return resolve(state.xbList)
      }
      getDictByBzdm(data).then((res) => {
        const list = res.data
        commit('SET_XB_LIST', list)
        resolve(list)
      }).catch((err) => { reject(err) })
    })
  },
  getDictByBzdm_cwf({ commit, state }, data) {
    return new Promise((resolve, reject) => {
      if (state.cwfList && state.cwfList.length) {
        return resolve(state.cwfList)
      }
      getDictByBzdm(data).then((res) => {
        const list = res.data
        commit('SET_CWF_LIST', list)
        resolve(list)
      }).catch((err) => { reject(err) })
    })
  },
  getDictByBzdm_xn({ commit, state }, data) {
    return new Promise((resolve, reject) => {
      if (state.xnList && state.xnList.length) {
        return resolve(state.xnList)
      }
      getDictByBzdm(data).then((res) => {
        const list = res.data
        commit('SET_XN_LIST', list)
        resolve(list)
      }).catch((err) => { reject(err) })
    })
  },
  setReqNum({ commit, state }, data) {
    if (state.reqNum === 0 && data === -1) {
      return
    }
    commit('SET_REQ_NUM', data)
  },
  setReqType({ commit }, data = 0) {
    commit('SET_REQ_TYPE', data)
  },
  buildJxjgTree({ commit, state }, data) {
    return new Promise((resolve, reject) => {
      if (state.jxjgTree && state.jxjgTree.length) {
        return resolve(state.jxjgTree)
      }
      buildJxjgTree(data).then((res) => {
        commit('SET_JXJG_TREE', res.data)
        resolve(res.data)
      }).catch((err) => { reject(err) })
    })
  },
  buildLyjgTree({ commit, state }, data) {
    return new Promise((resolve, reject) => {
      if (state.lyTree && state.lyTree.length) {
        return resolve(state.lyTree)
      }
      buildLyjgTree(data).then((res) => {
        commit('SET_LY_TREE', res.data)
        resolve(res.data)
      }).catch((err) => { reject(err) })
    })
  },
  buildLyjgTree2({ commit, state }, data) {
    return new Promise((resolve, reject) => {
      // if (state.lyTree2 && state.lyTree2.length) {
      //   return resolve(state.lyTree2)
      // }
      buildLyjgTree2(data).then((res) => {
        // commit('SET_LY_TREE_2', res.data)
        resolve(res.data)
      }).catch((err) => { reject(err) })
    })
  },
  getAllLyfl({ commit }, data) {
    return new Promise((resolve, reject) => {
      getAllLyfl(data).then((res) => {
        resolve(res.data)
      }).catch((err) => { reject(err) })
    })
  },
  getAllLy({ commit }, data) {
    return new Promise((resolve, reject) => {
      getAllLy(data).then((res) => {
        resolve(res.data)
      }).catch((err) => { reject(err) })
    })
  },
  getAllLc({ commit }, data) {
    return new Promise((resolve, reject) => {
      getAllLc(data).then((res) => {
        resolve(res.data)
      }).catch((err) => { reject(err) })
    })
  },
  getAllFj({ commit }, data) {
    return new Promise((resolve, reject) => {
      getAllFj(data).then((res) => {
        resolve(res.data)
      }).catch((err) => { reject(err) })
    })
  },
  getAllCw({ commit }, data) {
    return new Promise((resolve, reject) => {
      getAllCw(data).then((res) => {
        resolve(res.data)
      }).catch((err) => { reject(err) })
    })
  },
  getDictByBzdm_xz({ commit, state }, data) {
    return new Promise((resolve, reject) => {
      if (state.xzList && state.xzList.length) {
        return resolve(state.xzList)
      }
      getDictByBzdm(data).then((res) => {
        const list = res.data
        commit('SET_XZ_LIST', list)
        resolve(list)
      }).catch((err) => { reject(err) })
    })
  },
  getDictByBzdm_ssxq({ commit, state }, data) {
    return new Promise((resolve, reject) => {
      if (state.ssxqList && state.ssxqList.length) {
        return resolve(state.ssxqList)
      }
      getDictByBzdm(data).then((res) => {
        const list = res.data
        commit('SET_SSXQ_LIST', list)
        resolve(list)
      }).catch((err) => { reject(err) })
    })
  },
  getZzjgYxAll({ commit }, data) {
    return new Promise((resolve, reject) => {
      getZzjgYxAll(data).then((res) => {
        resolve(res)
      }).catch((err) => { reject(err) })
    })
  },
  getZzjgZyAll({ commit }, data) {
    return new Promise((resolve, reject) => {
      getZzjgZyAll(data).then((res) => {
        resolve(res)
      }).catch((err) => { reject(err) })
    })
  },
  buildLyflAndLyTree({ commit }, data) {
    return new Promise((resolve, reject) => {
      buildLyflAndLyTree(data).then((res) => {
        resolve(res)
      }).catch((err) => { reject(err) })
    })
  },
  buildLcAndFjTree({ commit }, data) {
    return new Promise((resolve, reject) => {
      buildLcAndFjTree(data).then((res) => {
        resolve(res)
      }).catch((err) => { reject(err) })
    })
  },
  findJzgxxList({ commit }, data) {
    return new Promise((resolve, reject) => {
      findJzgxxList(data).then((res) => {
        resolve(res)
      }).catch((err) => { reject(err) })
    })
  },
  printInterFace({ state }, data) {
    return new Promise((resolve, reject) => {
      printInterFace(data).then((res) => {
        resolve(res)
      }).catch((err) => { reject(err) })
    })
  }
}
export default {
  namespaced: true,
  state,
  mutations,
  actions
}
